﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Collections.Specialized;
using com.IronOne.BoardAppWeb.Controllers;
using com.IronOne.BoardAppWeb.Lang;
using com.IronOne.BoardAppWeb.Models.Meeting;
using com.IronOne.BoardAppWeb.Models.Entity;

namespace com.IronOne.BoardAppWeb.Models.Mappers
{
    public static class Mapper
    {
        #region Meetings

        /// <summary>Convert meeting model values to dictionary list values.</summary>
        ///<param name="entity">Job Entity</param>
        ///<param name="meeting"></param>
        ///<param name="user"></param>
        ///<returns>
        ///ListDictionary containing the tags and it's values
        ///</returns>      
        /// <remarks>
        /// This result of this method is used for generating the body string of the email.
        /// The template is placed at Views\Shared\Email Templates\MeetingEmailTemp.txt
        /// </remarks> 
        public static ListDictionary MeetingToDictionary(Meeting.MeetingModel meeting, User user)
        {
            //For Email only
            try
            {
                #region Old E-Mail code was commented
                /*
                //List Dictionary must contain only string values
                ListDictionary replacements = new ListDictionary();
                replacements.Add("<%Subject%>",getStringValue(meeting.Subject));

                if (meeting.Date!=null)
                {
                    //replacements.Add("<%Date%>", String.Format("{0:dd/MM/yyyy}",(DateTime) meeting.Date));
                    replacements.Add("<%Date%>", Formatter.GetDateOnlyFormat(meeting.Date));
                }
                else
                {
                    replacements.Add("<%Date%>", string.Empty);
                }

                replacements.Add("<%FirstName%>",getStringValue(user.FirstName));
                replacements.Add("<%SubCategoryName%>", getStringValue(meeting.SubCategoryName));                          
                // NTB UAT Requirement changes
                //replacements.Add("<%LastName%>", getStringValue(user.LastName));
                //replacements.Add("<%Description%>", getStringValue(meeting.Description)); 

                replacements.Add("<%MeetingType%>", getStringValue(meeting.meetingTypeName));  

                return replacements;
                */
                
                #endregion

                //List Dictionary must contain only string values
                ListDictionary replacements = new ListDictionary();
                replacements.Add("<%FirstName%>", getStringValue(user.FirstName));
                
                string bodyContent;
                if (meeting.meetingType == 0) //meeting
                {
                    bodyContent = Resources.info_emailBodyofMeeting+ " " + getStringValue(meeting.Subject) + ".";
                }
                else //circular
                {
                    bodyContent = Resources.info_emailBodyofCircular;
                }
                replacements.Add("<%MsgContent%>", bodyContent);
                return replacements;
            }
            catch (Exception)
            {
                throw new GenException("err_220_sys");
            }
        }

        public static MeetingModel MeetingEntityToModel(Entity.Meeting meetingEntity)
        {
            try
            {
                MeetingModel meetingModel = new MeetingModel();
                meetingModel.MeetingId = meetingEntity.MeetingId;
                meetingModel.Date = meetingEntity.Date;
                meetingModel.Subject = meetingEntity.Subject;
                meetingModel.Description = meetingEntity.Description;
                meetingModel.SubCategoryId = meetingEntity.SubCategoryId;
                meetingModel.SubCategoryName = meetingEntity.SubCategory.Name;
                meetingModel.CategoryId = meetingEntity.SubCategory.CategoryId;
                meetingModel.MeetingStatus = meetingEntity.Status;
                meetingModel.previosStatus = meetingEntity.Status;
                meetingModel.isTentative = meetingEntity.IsTentative;
                meetingModel.startTime = meetingEntity.StartTime.ToString();
              //  meetingModel.endTime =(meetingEntity.EndTime!=null)? meetingEntity.EndTime.ToString():null;
                meetingModel.endTime = meetingEntity.EndTime.ToString();
                meetingModel.meetingType = meetingEntity.MeetingType;

                return meetingModel;
            }
            catch (Exception)
            {
                // Error occured in Mapper
                throw new GenException("err_221_sys"); ;

            }
        }

        #endregion Meetings

        #region PrivateMethods

        /// <summary>get string value of any job field. This is used by ModelToDictionaryValues() for email purposes.</summary>
        /// <param name="jobfield"> any job field</param>
        ///<returns>
        ///string value of any field.
        ///</returns>      
        /// <remarks>
        /// This is the inverse of getInnerTextMethod
        /// </remarks> 
        private static string getStringValue(object value)
        {
            if (value == null)
            {
                return string.Empty;
            }
            else
            {
                return value.ToString();
            }
        }
        #endregion PrivateMethods
    }
}